//
//  YJAutomaticViewPager.m
//  CToolBox
//
//  Created by crj on 2016/11/28.
//  Copyright © 2016年 SC. All rights reserved.
//

#import "YJAutomaticViewPager.h"

#define kButtonBasicTag 100
#define kSelectedTag 200

@interface YJAutomaticViewPager()<UIScrollViewDelegate>

//选中下划线
@property (nonatomic, strong) UILabel *selectedLabel;
@property (nonatomic, assign) NSInteger selectIndex;
@property (nonatomic, strong) NSMutableArray *buttonArray;

@end

@implementation YJAutomaticViewPager

- (instancetype)initWithFrame:(CGRect)frame
                       titles:(NSArray<NSString *> *)titles
                        views:(NSArray<__kindof UIView *> *)views
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self configSelf];
        _views = views;
        _titleArray = titles;
        _buttonArray = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)configSelf{

    self.backgroundColor = [UIColor whiteColor];
    self.userInteractionEnabled = YES;
    _selectedColor = HEXCOLOR(kColorBlue1, 1);
    _noSelectedColor = HEXCOLOR(kColorGray3, 1);
    _menuHeight = 35;
    _menuFont = 15;
}

- (void)drawRect:(CGRect)rect{
    
    [self makeMenu:rect];
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, _menuHeight, rect.size.width, rect.size.height-_menuHeight)];
    _scrollView.userInteractionEnabled = YES;
    _scrollView.delegate = self;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.directionalLockEnabled = YES;
    _scrollView.bounces = NO;
    _scrollView.backgroundColor = [UIColor whiteColor];
    [_scrollView setContentSize:CGSizeMake(rect.size.width*_titleArray.count, _scrollView.frame.size.height)];
    
    [self addSubview:_scrollView];
}

- (void)makeMenu:(CGRect)rect{
    
    _menuScrollView = [[UIScrollView alloc] init];
    _menuScrollView.frame = CGRectMake(0, 0, rect.size.width, _menuHeight);
    _menuScrollView.showsVerticalScrollIndicator = NO;
    _menuScrollView.showsHorizontalScrollIndicator = NO;
    _menuScrollView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_menuScrollView];
    
    UIView *lastView = nil;
    for(int i=0; i<_titleArray.count; i++){
        NSString *title = _titleArray[i];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i;
        button.titleLabel.font = [UIFont systemFontOfSize:_menuFont];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:HEXCOLOR(kColorGray3, 1) forState:UIControlStateNormal];
        [button setTitleColor:HEXCOLOR(kColorBlue1, 1) forState:UIControlStateSelected];
        [button addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
        if(lastView == nil){
            button.frame = CGRectMake(kSpaceSide, 0, 0, 30);
        }else{
            button.frame = CGRectMake(lastView.yj_right+kSpaceSide, 0, 0, 30);
        }
        [button sizeToFit];
        
        
        lastView = button;
        [_scrollView addSubview:button];
        
        [_buttonArray addObject:button];
    }
    
    _scrollView.contentSize = CGSizeMake(lastView.yj_right+kSpaceSide, 0);
    [_scrollView addSubview:self.selectedLabel];


}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    
}

#pragma mark - GET

- (UILabel *)selectedLabel{
    
    if(!_selectedLabel){
        _selectedLabel = [[UILabel alloc] init];
        _selectedLabel.backgroundColor = HEXCOLOR(kColorBlue1, 1);
        _selectedLabel.tag = kSelectedTag;
        [_menuScrollView addSubview:_selectedLabel];
    }
    return _selectedLabel;
}



@end
